const Auth = require("../models/auth.js");
const jsonwebtoken = require("jsonwebtoken");
const {secret} = require("../config");

class UserController {
	async find(ctx) {  // 查询用户列表(分页)
		const {pageSize = 10,pageIndex = 1} = ctx.query;
		const page = Math.max(pageIndex, 1) - 1;
		const perPage = Math.max(pageSize, 1);
		const total = await Auth.count()
		const data = await Auth.find({name: new RegExp(ctx.query.q)})
			.limit(perPage)
			.skip(page * perPage);
		ctx.body = {
			code:0,
			data:{
				total,
				data
			}
		}
	}
	async findByToken(ctx){
		let token = ctx.headers.authorization.split(' ')[1]
		await jsonwebtoken.verify(token,secret,async(err,res)=>{
			const {id} = res
			const user = await Auth.findOne({id})
			if (!user) {
				ctx.throw(404, "用户不存在");
			}
			ctx.body = {
				code:0,
				data:user
			}
		})
	}
	async findById(ctx) {  // 根据id查询特定用户
		const {fields} = ctx.query;
		const selectFields =  // 查询条件
			fields &&
			fields
				.split(";")
				.filter(f => f)
				.map(f => " +" + f)
				.join("");
		const populateStr =  // 展示字段
			fields &&
			fields
				.split(";")
				.filter(f => f)
				.map(f => {
					if (f === "employments") {
						return "employments.company employments.job";
					}
					if (f === "educations") {
						return "educations.school educations.major";
					}
					return f;
				})
				.join(" ");
		const user = await Auth.find({id:ctx.params.id})
			.select(selectFields)
			.populate(populateStr);
		if (!user) {
			ctx.throw(404, "用户不存在");
		}
		ctx.body = user;
	}

	async create(ctx) {  // 创建用户
		ctx.verifyParams({  // 入参格式校验
			name: {type: "string", required: true},
			password: {type: "string", required: true}
		});
		const {name} = ctx.request.body;
		const repeatedUser = await Auth.findOne({name});
		if (repeatedUser) {  // 校验用户名是否已存在
			ctx.throw(409, "用户名已存在");
		}
		const user = await new Auth(ctx.request.body).save();
		ctx.body = {
			code: 0,
			data: user,
			message: '用户创建成功'
		};
	}

	async checkOwner(ctx, next) {  // 判断用户身份合法性
		if (ctx.params.id !== ctx.state.user.id) {
			ctx.throw(403, "没有权限");
		}
		await next();
	}

	async update(ctx) {  // 更新用户信息
		ctx.verifyParams({
			name: {type: "string", required: false},
			password: {type: "string", required: false},
			avatar_url: {type: "string", required: false},
			rule: {type: Array, required: false}
		});
		const user = await Auth.findByIdAndUpdate(ctx.params.id, ctx.request.body);
		if (!user) {
			ctx.throw(404, "用户不存在");
		}
		ctx.body = user;
	}

	async delete(ctx) {  // 删除用户
		const user = await Auth.findByIdAndRemove(ctx.params.id);
		if (!user) {
			ctx.throw(404, "用户不存在");
		}
		ctx.status = 204;
	}

	async login(ctx) {  // 登录
		ctx.verifyParams({
			name: {type: "string", required: true},
			password: {type: "string", required: true}
		});
		const user = await Auth.findOne(ctx.request.body);
		if (!user) {
			ctx.throw(401, "用户名或密码不正确");
		}
		const {id, name} = user;
		const token = jsonwebtoken.sign({id, name}, secret, {expiresIn: "1d"}, {arguments: 'HS256'});  // 登录成功返回jwt加密后的token信息
		ctx.body = {
			code: 0,
			data: {
				token,
			},
		};
	}
	async refreshToken(ctx){
		let token = ctx.headers.authorization.split(' ')[1]
		await jsonwebtoken.verify(token,secret,async(err,res)=>{
			let newToken = token;
			if(err) {
				const {id, name} = res;
				newToken = jsonwebtoken.sign({id, name}, secret, {expiresIn: "1d"}, {arguments: 'HS256'});  // 登录成功返回jwt加密后的token信息
			}
			ctx.body = {
				code: 0,
				data: {
					token:newToken,
				},
			};

		})
	}
	async checkUserExist(ctx, next) {  // 查询用户是否存在
		const user = await Auth.findById(ctx.params.id);
		if (!user) {
			ctx.throw(404, "用户不存在");
		}
		await next();
	}

}

module.exports = new UserController();
